package com.flower.controller;

import com.flower.model.dto.RoleDto;
import com.flower.model.dto.RolePageQueryDto;
import com.flower.model.vo.RoleVo;
import com.flower.result.PageResult;
import com.flower.result.Result;
import com.flower.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@SuppressWarnings("all")
/**
 *@ClassName RoleController
 *@Description TODO
 *@Author @O_o  GW__
 *@Date 2025/6/12 16:44
 *@Version 1.0
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping
    @ApiOperation("获取所有角色")
    public Result<List<RoleVo>> getAllRole() {
        log.info("获取所有角色");
        List<RoleVo> roleVos = roleService.getAllRole();
        return Result.success(roleVos);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> getRoleList(RolePageQueryDto rolePageQueryDto) {
        log.info("分页查询");
        PageResult roleVos = roleService.getRoleList(rolePageQueryDto);
        return Result.success(roleVos);
    }

    @PostMapping
    @ApiOperation("修改/添加")
    public Result<String> updateRole(@RequestBody RoleDto roleDto) {
        log.info("修改/添加");
        roleService.updateRole(roleDto);
        return Result.success("保存成功");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public Result<String> deleteRole(@PathVariable Integer id) {
        log.info("删除");
        roleService.deleteRole(id);
        return Result.success("删除成功");
    }

    @DeleteMapping("del/batch")
    @ApiOperation("批量删除")
    public Result<String> deleteRoleList(@RequestParam List<Integer> ids) {
        log.info("批量删除");
        roleService.deleteRoleList(ids);
        return Result.success("批量删除成功");
    }

    @GetMapping("/roleMenu/{roleld}")
    @ApiOperation("获取角色已分配菜单接口")
    public Result<List<Integer>> getRoleMenu(@PathVariable Integer roleld) {
        log.info("获取角色已分配菜单接口");
        List<Integer> roleMenu = roleService.getRoleMenu(roleld);
        return Result.success(roleMenu);
    }

    @PostMapping("/roleMenu/{roleld}")
    @ApiOperation("保存角色已分配菜单接口")
    public Result<String> saveRoleMenu(@PathVariable Integer roleld, @RequestBody List<Integer> menuIds) {
        log.info("保存角色已分配菜单接口");
        roleService.updateRoleMenu(roleld, menuIds);
        return Result.success("绑定成功");
    }
}
